package edu.ucsd.cse135.gas.action.applicant;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.omg.CORBA.CODESET_INCOMPATIBLE;

import edu.ucsd.cse135.gas.bean.applicant.DisciplineForm;
import edu.ucsd.cse135.gas.logic.Degree;
import edu.ucsd.cse135.gas.logic.support.Discipline;
import edu.ucsd.cse135.gas.resources.Constants;

public class ChooseDisciplineAction extends Action {

	public ChooseDisciplineAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		DisciplineForm disciplineForm = (DisciplineForm)form;
		ArrayList<Degree> degreeList = (ArrayList<Degree>)request.getSession().getAttribute("degrees");
		
		Degree degree = (Degree)request.getSession().getAttribute("degree");
		
		if(disciplineForm.getDisciplineId() == 0)
		{
			degree.setDisciplineId(Discipline.storeCustomDiscipline(disciplineForm.getDiscipline()));
			degree.setDiscipline(disciplineForm.getDiscipline());
		}
		else
		{
			degree.setDiscipline(disciplineForm.getDiscipline());
		}
		
		degree.setMonth(disciplineForm.getMonth());
		degree.setYear(disciplineForm.getYear());
		degree.setGpa(disciplineForm.getGpa());
		degree.setTitle(disciplineForm.getTitle());
		
		degreeList.add(degree);
		
		request.getSession().setAttribute("degrees", degreeList);
		
		return mapping.findForward(Constants.SUCCESS);
		
	}
}